package com.tanhua.sso.service;

import cn.hutool.core.util.StrUtil;
import com.tanhua.common.service.PicUploadService;
import com.tanhua.common.vo.ErrorResult;
import com.tanhua.common.vo.PicUploadResult;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.enums.SexEnum;
import com.tanhua.dubbo.pojo.UserInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@Service
public class UserInfoService {

    @DubboReference(version = "1.0.0")
    private UserInfoApi userInfoApi;

    @Autowired
    private UserService userService;

    @Autowired
    private PicUploadService picUploadService;

    @Autowired
    private FaceImageService faceImageService;

    /**
     * 更新用户基本数据
     *
     * @param param
     * @param token
     * @return
     */
    public Object loginRegInfo(Map<String, String> param, String token) {
        Long userId = this.userService.checkToken(token);
        if (null == userId) {
            return ErrorResult.builder().errCode("401")
                    .errMessage("用户token已失效，请重新登录！").build();
        }

        // 判断用户信息是否存在，否则可能会出现系统漏洞
        UserInfo userInfo = this.userInfoApi.queryByUserId(userId);
        if(null == userInfo){
            return null;
        }
        userInfo.setNickName(param.get("nickname"));
        userInfo.setBirthday(param.get("birthday"));
        userInfo.setCity(param.get("city"));
        userInfo.setSex(StrUtil.equals(param.get("gender"), "man") ? SexEnum.MAN : SexEnum.WOMAN);
        userInfo.setUserId(userId);

        Boolean result = this.userInfoApi.save(userInfo);
        if (result) {
            return null;
        }
        return ErrorResult.builder().errCode("500")
                .errMessage("用户信息更新失败，请检查数据！").build();
    }

    /**
     * 更新头像
     *
     * @param multipartFile
     * @param token
     * @return
     */
    public Object loginRegInfoLogo(MultipartFile multipartFile, String token) {
        Long userId = this.userService.checkToken(token);
        if (null == userId) {
            return ErrorResult.builder().errCode("401")
                    .errMessage("用户token已失效，请重新登录！").build();
        }

        //上传头像到阿里云OSS
        PicUploadResult uploadResult = this.picUploadService.upload(multipartFile);
        if (StrUtil.isEmpty(uploadResult.getName())) {
            return ErrorResult.builder().errCode("500")
                    .errMessage("用户头像上传失败，请重新上传！").build();
        }

        //校验头像是否是人像
        if (!this.faceImageService.checkIsPortrait(uploadResult.getName())) {
            // 删除已上传到图片
            this.picUploadService.deletePic(uploadResult.getName());
            return ErrorResult.builder().errCode("500")
                    .errMessage("用户头像非人像，请重新上传！").build();
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userId);
        userInfo.setLogo(uploadResult.getName());
        userInfo.setCoverPic(uploadResult.getName());
        Boolean result = this.userInfoApi.update(userInfo);
        if (result) {
            return null;
        }
        return ErrorResult.builder().errCode("500")
                .errMessage("用户头像上传失败，请重新上传！").build();
    }
}
